<!DOCTYPE html>
<html lang="zh_CN">

<head>
   <#include "../common/header.ftl">
    <script src="/js/plugins/ckeditor/ckeditor.js"></script>

    <script>
        $(function () {

            //富文本编辑框
            var ck = CKEDITOR.replace( 'editor',{
                //图片预览服务器:存放之前传过图片
                filebrowserBrowseUrl: '/xjksjdkfjksdjf',
                //图片上传请求路径
                filebrowserUploadUrl: '/upload.do'
            });

            $("#strategyId").change(function () {

                var val = $(this).val();
                if (val) {
                    if (val == -1) {
                        var html = "<option value='-1'>--请选择--</option>";
                        $("#CatalogId").html(html);
                        console.log(html);
                        return;
                    }

                    $.get("/strategyCatalog/getStrategyCatalogByStrategyId.do", {"strategyId": val}, function (data) {
                        console.log(data);
                        html = "<option value='-1'>--请选择--</option>";
                        $.each(data, function (index, ele) {
                            html += "<option value='" + ele.id + "'>" + ele.name + "</option>";
                        })
                        console.log(html);
                        $("#CatalogId").html(html);
                    })
                }

            })


            $("#addStrategyBtn").click(function () {
                $("#editForm").clearForm(true);
                $("#inputModal").modal("show");
            })

            //保存操作
            $(".saveBtn").click(function () {
                //把富文本框的内容设置进文本域
                var value = ck.getData();
                $("#editor").val(value);
                $("#editForm").ajaxSubmit(function (data) {
                    if (data.success) {
                        window.location.reload();
                    }
                })
            })

            //修改操作
            $(".edit_Btn").click(function () {
                var json = $(this).data("json");
                console.log(json);
                if (json) {

                    //攻略文章数据的回显
                    $("#editForm input[name='id']").val(json.id);
                    $("#editForm input[name='title']").val(json.title);

                    $("#editForm input[name='coverUrl']").val(json.coverUrl);
                    $("#editForm img").attr("src",json.coverUrl);

                    $("#editForm select[name='state']").val(json.state);
                    $("#editForm input[name='sequence']").val(json.sequence);
                    //所属攻略的回显
                    $("#strategyId").val(json.strategyId);
                    //攻略类别的回显
                    $.get("/strategyCatalog/getStrategyCatalogByStrategyId.do", {"strategyId": json.strategyId}, function (data) {
                        console.log(data);
                        var html = "<option value='-1'>--请选择--</option>";
                        $.each(data, function (index, ele) {
                            html += "<option value='" + ele.id + "'>" + ele.name + "</option>";
                        })
                        console.log(html);
                        $("#CatalogId").html(html);
                        $("#CatalogId").val(json.catalogId);
                    })
                    //大文本内容的回显
                    $.get("/strategyDetail/getStrategyContentByStrategyId.do",{"strategyId":json.id},function (data) {
                        console.log(data);
                        $("#editor").val(data.content);
                        //通过插件对象设置内容
                        ck.setData($("#editor"))
                    })

                }
                $("#inputModal").modal("show");
            })


        })
    </script>


</head>

<body>

<!--模态框-->
<div class="modal fade" id="inputModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">攻略文章编辑/添加</h4>
            </div>
            <div class="modal-body">

                <form class="form-horizontal" action="/strategyDetail/saveOrUpdate.do" method="post"
                      id="editForm" enctype="multipart/form-data">
                    <input type="hidden" name="id">
                    <input type="hidden" name="travel.id">

                    <div class="form-group">
                        <label class="col-sm-4 control-label">文章标题</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="title" placeholder="文章标题">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-4 control-label">封面：</label>
                        <div class="col-sm-6">
                            <img src="#" width="100px">
                            <input type="hidden" class="form-control" name="coverUrl">
                            <input type="file" class="form-control" name="pic">
                        </div>
                    </div>


                    <div class="form-group">
                        <label class="col-sm-4 control-label">所属攻略：</label>
                        <div class="col-sm-6">
                            <select class="form-control" id="strategyId" name="strategy.id" placeholder="请选择所属攻略">
                                <option value="-1">--请选择--</option>
                                <#if strategies??>
                                    <#list strategies as s>
                                <option value="${s.id}">${s.title}</option>
                                    </#list>
                                </#if>
                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-4 control-label">攻略类别：</label>
                        <div class="col-sm-6">
                            <select class="form-control" id="CatalogId" name="catalog.id"
                                    placeholder="请选择所属攻略">

                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-4 control-label">是否发布：</label>
                        <div class="col-sm-6">
                            <select class="form-control" name="state">
                                <option value="1">不发布</option>
                                <option value="2">发布</option>
                            </select>
                        </div>
                    </div>


                    <div class="form-group">
                        <label class="col-sm-4 control-label">排序:</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="sequence" placeholder="排序">
                        </div>
                    </div>
                    <!--富文本框关联的文本域-->
                    <textarea name="strategyContent.content" id="editor" rows="10" cols="80">

                    </textarea>


                </form>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary saveBtn">保存</button>
            </div>
        </div>
    </div>
</div>


<div class="container " style="margin-top: 20px">
    <#include "../common/top.ftl">
    <div class="row">
        <div class="col-sm-3">
        <#--设置当前要回显当前菜单,必须在载人菜单前完成设置-->
            <c:set var="menu" value="department"/>
                <#assign menu = "strategyDetail" >
           <#include "../common/menu.ftl">
        </div>
        <div class="col-sm-9">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">攻略文章管理</h1>
                </div>
            </div>

            <!--高级查询--->
            <form class="form-inline" id="searchForm" action="" method="post">
                <input type="hidden" id="currentPage" name="currentPage" value="1">

                <!--高级查询--->
                <form class="form-inline" id="searchForm" action="/travel/list.do" method="post">
                    <input type="hidden" id="currentPage" name="currentPage" value="1">
                    <div class="form-group">
                        <label>关键字:</label>
                        <input type="text" class="form-control" name="keyword" value="${(qo.keyword)!}"
                               placeholder="请输入标题">
                    </div>


                    <button type="submit" class="btn btn-primary">
                        <span class="glyphicon glyphicon-search"></span> 查询
                    </button>

                    <a href="javascript:void(-1);" class="btn btn-success" id="addStrategyBtn">添加攻略分类</a>
                </form>

                <table class="table table-striped table-hover">
                    <tr>
                        <th>标题</th>
                        <th>封面</th>
                        <th>发布时间</th>
                        <th>排序</th>
                        <th>攻略类别</th>
                        <th>状态</th>
                    </tr>
                    <!--如果result不为空就迭代输出-->
                    <#if result??>
                        <#list result.list as entity>

                        <tr>
                            <td>${(entity.title)!}</td>
                            <td><img src="${entity.coverUrl}" width="50px"></td>
                            <td>${(entity.releaseTime?string("yyyy-MM-dd"))!}</td>
                            <td>${(entity.catalog.name)!}</td>
                            <td>${(entity.stateName)!}</td>

                            <td>
                                <a href="javascript:void(-1);" data-json='${entity.JSONString}' class="edit_Btn">修改</a>
                            </td>

                        </tr>

                        </#list>
                    </#if>
                </table>
                <!--分页-->
            <#include "../common/page.ftl">
        </div>
    </div>
</div>

<script>

</script>

</body>

</html>